package com.andy.module.util.choose

import android.app.Dialog
import android.content.Context
import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import com.afollestad.materialdialogs.utils.MDUtil.getStringArray
import com.andy.module.R
import com.andy.module.bean.CodeBean
import com.andy.ui.ext.clickNoRepeat

/**
 *@Author: luofan
 *@CreateDate: 2021/1/27 11:31
 *@Description:
 */
class CodeChooseManager(val chooseText: TextView, val dialogTitle: String) {

    private var chooseCode: CodeBean? = null

    var chooseListener: ((code: CodeBean) -> Unit)? = null

    private var dialog: Dialog? = null

    private var dataSource = mutableListOf<CodeBean>()

    private var dataLoader: CodeLoader? = null

    private var groupKey = ""

    private var hasInit = false

    init {
        chooseText.clickNoRepeat {
            if (hasInit) {
                showDialog(dataSource)
            } else {
                dataLoader?.getData(groupKey) {
                    hasInit = true
                    dataSource.clear()
                    dataSource.addAll(it)
                    showDialog(dataSource)
                }
            }
        }
    }

    fun setDataLoader(dataLoader: CodeLoader, groupKey: String) {
        this.dataLoader = dataLoader
        this.groupKey = groupKey
    }

    fun setDataSource(dataSource: MutableList<CodeBean>) {
        this.dataSource = dataSource
        hasInit = true
    }

    fun initChooseCode(code: String, value: String) {
        val codeBean = CodeBean()
        codeBean.code = code
        codeBean.value = value
        chooseCode = codeBean
        chooseText.text = value
    }

    fun getChooseCode() = chooseCode

    private fun showDialog(codeSource: MutableList<CodeBean>) {
        if (dialog == null) {
            val itemList = codeSource.map { it.value }
            dialog = MaterialDialog(chooseText.context)
                .title(text = dialogTitle)
                .cancelable(true)
                .cancelOnTouchOutside(false)
                .cornerRadius(12f)
                //customListAdapter 可以自定义adapter
                .listItemsSingleChoice(items = itemList) { dialog, index, text ->
                    chooseCode = codeSource[index]
                    chooseText.text = text
                    chooseListener?.let {
                        it.invoke(codeSource[index])
                    }
                }
        }
        dialog?.show()
    }

    companion object {

        fun genYesNoCodeList(context: Context): MutableList<CodeBean> {
            val list = mutableListOf<CodeBean>()
            val array = context.getStringArray(R.array.yes_no)
            array.forEachIndexed { index, str ->
                val bean = CodeBean()
                bean.code = index.toString()
                bean.value = str
                list.add(bean)
            }
            return list
        }
    }

    interface CodeLoader {
        fun getData(groupKey: String, success: ((list: List<CodeBean>) -> Unit))
    }
}